package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.LocalPhotoPagerAdapter;
import com.hxkcloud.patient.views.PhotoViewPager;

import java.util.ArrayList;

/**
 * Created by william_sun on 2016-07-15.
 * Description:图片查看页
 */
public class PhotosActivity extends BaseActivity implements View.OnClickListener {
    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_del;

    private PhotoViewPager viewPager;

    private ArrayList<String> Urls; //图片url
    private int index;
    private boolean canDel = true;//是否可以选择删除

    private LocalPhotoPagerAdapter adapter;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photos);

        index = getIntent().getIntExtra("index", 0);
        Urls = getIntent().getStringArrayListExtra("urls");
        canDel = getIntent().getBooleanExtra("canDel", true);


        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_del = (TextView) findViewById(R.id.tv_title_func);

        viewPager = (PhotoViewPager) findViewById(R.id.vp_photos);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText((index + 1) + "/" + Urls.size());
        iv_back.setOnClickListener(this);
        if (canDel) {
            tv_del.setVisibility(View.VISIBLE);
            tv_del.setText(R.string.delete);
            tv_del.setOnClickListener(this);
        } else {
            tv_del.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void initListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_title.setText((position + 1) + "/" + Urls.size());
                index = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
        adapter = new LocalPhotoPagerAdapter(mContext, Urls);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(index);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_func:
                Intent iBack = new Intent(mContext, AddInspectionReportActivity.class);
                iBack.putExtra("index", index);
                setResult(RESULT_OK, iBack);
                finish();
                break;
        }
    }
}
